from django.test import TestCase
from django.test.client import Client

from mdcssport.mdcsapp.models import MatchComment 

"""
Testira komentarisanje meceva
"""
class MatchCommentsTestCase(TestCase):
    
    def setUp(self):
        pass

    def runTest(self):
        self.testViewMatchPage()
        self.testAddComment()
        self.testEditComment()

    def testViewMatchPage(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.get("/mdcs/match/1/")
        self.failIf(response == None)
        self.failIf(response.context == None)
        self.failUnlessEqual(len(response.context), 2)
        self.failIf(response.context[0]["match"] == None)
        
        match = response.context[0]["match"]
        self.assertEqual(match.id, 1)
        self.assertEqual(len(match.matchcomment_set.all()), 2)
    
    def testAddComment(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.post("/mdcs/match/1/add_comment/",
            {'comment_text': 'tekst komentara'})
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        
        comments = MatchComment.objects.filter(match_id=1).order_by('-id')
        self.assertEqual(len(comments), 3)
        last_comment = comments[0]
        self.assertEqual(last_comment.comment_text, "tekst komentara")
        self.assertEqual(last_comment.comment_author.id, 2)
        self.assertEqual(last_comment.match.id, 1)

    def testEditComment(self):
        c = Client()
        c.login(username='kokan', password='123')
        
        response = c.post("/mdcs/match/1/edit_comment/1/",
            {'comment_text': 'izmenjen tekst komentara'})
        self.failIf(response == None)
        self.failUnlessEqual(response.status_code, 302)
        
        comments = MatchComment.objects.filter(match_id=1).order_by('-date_updated', 'id')
        self.assertEqual(len(comments), 3)
        last_comment = comments[0]
        self.assertEqual(last_comment.comment_text, "izmenjen tekst komentara")
        self.assertEqual(last_comment.comment_author.id, 2)
        self.assertEqual(last_comment.match.id, 1)
        
        next_comment = comments[1]
        self.assertEqual(next_comment.comment_text, "Stvarno, svaka nam cast")
        self.assertTrue(next_comment.date_updated == None)

        next_comment = comments[2]
        self.assertEqual(next_comment.comment_text, "tekst komentara")
        self.assertTrue(next_comment.date_updated == None)